
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    id: "",
    data: "",
    commentList: [],
    pageIndex: 1,
    state: "",
    evaluateValue: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      id: options.id
    })
    this.GetStateBarInfo();
    this.GetDisscussList();
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetDisscussList();
  },

  // 
  GetStateBarInfo: function() {
    var that = this;
    var uid = this.data.uid;
    var id = this.data.id;
    var data = this.data.data;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetStateBarInfo&UID=" + uid + "&BarId=" + id),
      success: function(res) {
        data = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        data.Images = data.Images.split(";");
        console.log(data);
        that.setData({
          data: data,
        })
      }
    })
  },
  // 
  GetDisscussList: function() {
    var that = this;
    var uid = this.data.uid;
    var id = this.data.id;
    var commentList = this.data.commentList;
    var pageIndex = this.data.pageIndex;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetDisscussList&BarId=" + id + "&PageSize=10&PageIndex=" + pageIndex),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          commentList.push(json[i])
        }
        that.setData({
          commentList: commentList,
        })
        console.log(commentList);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
      }
    })
  },
  // 评论
  evaluate: function(e) {
    this.setData({
      evaluateValue: e.detail.value
    })
  },
  // 发送评论
  sendEvaluate: function() {
    var that = this;
    var uid = this.data.uid;
    var id = this.data.id;
    var evaluateValue = this.data.evaluateValue;
    var commentList = this.data.commentList;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=DiscussBar&UID=" + uid + "&BarId=" + id + "&Content=" + evaluateValue),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: "评论成功！",
            icon: 'none',
            duration: 2000
          });
          that.data.pageIndex = 1;
          that.data.commentList = [],
            that.GetDisscussList();
          that.setData({
            evaluateValue: "",
          })
        } else {
          wx.showToast({
            title: "请输入要评论的内容！",
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 点赞
  dianZanTap: function() {
    var that = this;
    var uid = this.data.uid;
    var id = this.data.id;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=StateBarClickLike&UID=" + uid + "&BarId=" + id),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultData == '1') {
          wx.showToast({
            title: "点赞成功！",
            icon: 'none',
            duration: 2000
          });
          that.GetStateBarInfo();
        } else {
          wx.showToast({
            title: "已经点过赞了！",
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
})